home *** CD-ROM | disk | FTP | other *** search
/ Amiga News 95 / Amiga News 95.iso / dpat / dpat06 / itex / itex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-09  |  10.4 KB  |  400 lines

  1. /************************************************************************
  2. *     ITex   Pierre Baillargeon 1989/ Michel Laliberté, mai 1991       *
  3. *   cc +L <source>.c                                                    *
  4. *   ln +Cdb <source>.o -lc32                                            *
  5. ************************************************************************/
  6. #include <exec/types.h>
  7. #include <exec/io.h>
  8. #include <exec/memory.h>
  9. #include <libraries/dos.h>
  10. #include <libraries/dosextens.h>
  11. #include <graphics/gfxbase.h>
  12. #include <intuition/intuition.h>
  13. #include <intuition/preferences.h>
  14. #include <workbench/startup.h>
  15. #include <workbench/workbench.h>
  16. #include <workbench/icon.h>
  17. #include <functions.h>
  18.  
  19. struct WBStartup *WBenchMsg;
  20. struct DiskObject *diskobj;
  21. struct WBArg *wbarg;
  22.  
  23. static WORD bordbox[] = {8,2,8,4,0,4,0,0,8,0,8,2,20,2,20,8,4,8,4,4};
  24. static struct Border iconbord = {    /*  Icon border  */
  25.      1, 0, 1, 0, JAM1,10,bordbox,NULL,
  26. };
  27.  
  28. struct Gadget IconGadget = {  
  29.     NULL,5,NULL,22,10,NULL,    
  30.     RELVERIFY+GADGIMMEDIATE+GADGHCOMP,    
  31.     BOOLGADGET,(APTR)&iconbord,    
  32.     NULL,NULL,NULL,NULL,8,NULL    
  33. };
  34.  
  35. struct NewWindow NW = {
  36.     0,0,0,0,0,1,
  37.     MOUSEBUTTONS | RAWKEY | NEWSIZE | CLOSEWINDOW | GADGETUP,
  38.     SMART_REFRESH | NOCAREREFRESH | ACTIVATE | RMBTRAP |
  39.     WINDOWSIZING | SIZEBBOTTOM | WINDOWDRAG | WINDOWCLOSE | WINDOWDEPTH,
  40.     &IconGadget,0L,0L,0L,0L,
  41.     50,50,-1,-1,WBENCHSCREEN };
  42.  
  43. struct IntuiText Mess = {
  44.     2,0,JAM2,0,0,NULL,
  45.     (UBYTE *)"    Use Mouse or Up/Down Cursors »*« FAST: Shift-Cursor »*«  [ESC] to QUIT",
  46.     NULL    
  47. };
  48.  
  49. struct Preferences prefs;
  50.  
  51. WORD    X, Y, k,fr;
  52. LONG    i, j;
  53. char    fname[31],temp[95];
  54. BYTE    *Buffer, **Memory;
  55. struct    Screen *Screen;
  56. struct    Window *Window;
  57. struct    RastPort *RastPort;
  58. struct    IntuiMessage    *Msg;
  59. LONG    StartTopLine, CurrentTopLine, Len, Number = 1L, MaxLine, MaxColumn;
  60. struct    IntuitionBase *IntuitionBase;
  61. struct    IconBase *IconBase;
  62. struct    GfxBase *GfxBase;
  63.  
  64. unsigned char fvar[4][10] = {"File: ","Line: ","Fichier: ","Ligne: "};
  65.  
  66. _abort(){}
  67.  
  68. main (argc,argv)
  69. WORD argc;
  70. char *argv[];
  71. {
  72.     struct    FileHandle *File;
  73.     APTR    Object;
  74.     ULONG    Class;
  75.     UWORD    Code, Qualifier, iconx = 560, icony = 0;
  76.     BOOL    chg=TRUE;
  77.     char    **toolptr, **findtools();
  78.     
  79.  
  80.     IntuitionBase = (struct IntuitionBase *)
  81.             OpenLibrary("intuition.library", 0L);
  82.     if (IntuitionBase == NULL) {
  83.         exit(1);
  84.     }
  85.        if ((GfxBase = (void *)OpenLibrary("graphics.library",0)) == NULL) {
  86.         exit();
  87.     }
  88.     IconBase = (struct IconBase *)OpenLibrary("icon.library",0L);
  89.     if ((IconBase) == NULL ) 
  90.         exit(1);
  91.     if (argc) {                                /* Lancé du CLI */
  92.         toolptr = findtools(argv[0],((struct Process *)
  93.                         FindTask(NULL))->pr_CurrentDir);
  94.         if ( !(File = Open(argv[1],MODE_OLDFILE))) {
  95.             exit (1);
  96.         }
  97.         strcat(fname,argv[1]);
  98.     }
  99.     else {                            /*    Open the file - Workbench */
  100.         if (WBenchMsg->sm_NumArgs == 1) exit (1);
  101.         wbarg = &(WBenchMsg->sm_ArgList[1]);
  102.         CurrentDir(wbarg->wa_Lock);
  103.         if ( !(File = Open(wbarg->wa_Name,MODE_OLDFILE)))        {
  104.             exit (1);
  105.         }
  106.         strcat(fname,wbarg->wa_Name);
  107.         wbarg = &(WBenchMsg->sm_ArgList[WBenchMsg->sm_NumArgs-1]);
  108.         toolptr = findtools(wbarg->wa_Name,wbarg->wa_Lock);
  109.     }
  110.     if ((FindToolType(toolptr, "FR"))[0] == '1') {
  111.         fr = 2;
  112.         Mess.IText = (UBYTE *)"    HAUT/BAS: Souris ou curseurs »*« PAGE: [Shift] Curseur »*«  FIN: [ESC]";
  113.     }
  114.     NW.Height = GfxBase->NormalDisplayRows; /*    Open the Window. */
  115.     NW.Width = GfxBase->NormalDisplayColumns;
  116.     GetPrefs(&prefs,sizeof(struct Preferences));
  117.     if (prefs.LaceWB) NW.Height += NW.Height;
  118.     MaxColumn = (NW.Width - 6) / 8;
  119.     MaxLine = (NW.Height - 22) / 8;
  120.     NW.Title = (UBYTE *) fname;
  121.     if( !(Window = OpenWindow(&NW))) {
  122.         CleanUp();    
  123.     }
  124.     RastPort = Window->RPort;
  125.     PrintIText(RastPort, &Mess, 10, NW.Height-10);    
  126.     SetAPen (RastPort, 0);
  127.     RectFill (RastPort,2,Window->Height-10,28,Window->Height-2);
  128.     SetAPen (RastPort, 1);
  129.     IconGadget.TopEdge = Window->Height-10;
  130.     RefreshGList(&IconGadget,Window,NULL,1);
  131.     
  132.     Len = Seek (File, 0L, OFFSET_END);  /*    Find its lenght. */
  133.     Len = Seek (File, 0L, OFFSET_BEGINNING);
  134.     if ( !(Buffer = (BYTE *)AllocMem(Len + 1, MEMF_CLEAR)))    {
  135.         Close (File);                    /*    Alloc the memory needed.*/
  136.         CleanUp();
  137.     }
  138.     Buffer[Len] = 10;
  139.     Read (File, Buffer, Len);           /*    Read and Close it. */
  140.     Close (File);
  141.     for (i = 0; i < Len; i += 1L){      /*    Find Number of lines. */
  142.         if (Buffer[i] == 10)
  143.             Number += 1L;
  144.     }
  145.     if ( !(Memory = (BYTE **)AllocMem(Number * 4L, MEMF_CLEAR)))    {
  146.         CleanUp();        /*    Alloc memory to hold pointers to the line. */
  147.     }
  148.     i = j = 0;                            /*    Find all lines' beginning. */
  149.     Memory[j++] = Buffer;
  150.     while(i < Len)    {
  151.         if(Buffer[i] == 10)       {/*** Check for Line-Feed ***/
  152.                 Memory[j++] = &Buffer[i + 1];
  153.         }
  154.         i++;
  155.     }
  156.     
  157.     CurrentTopLine = 0L;
  158.  
  159.     for ( j = 0L; j < Number && j < MaxLine; j++)    {
  160.         WriteLine(j, (LONG)(j * 8 + 17));    /*Fill the screen with text.*/
  161.     }
  162.     for(;;)    {    /*    Now wait forever (kinda) for user input. */
  163.         if (chg) {    
  164.             sprintf(temp,"ITex v1.0   %s%s  %s %d / %d ",fvar[0+fr],fname,
  165.                                     fvar[1+fr],CurrentTopLine+MaxLine,Number);
  166.             SetWindowTitles(Window,temp,-1);
  167.             chg = FALSE;
  168.         }
  169.         WaitPort(Window->UserPort);
  170.         while ((Msg = (struct IntuiMessage *)GetMsg(Window->UserPort)) != 0L)
  171.         {
  172.             Class = Msg->Class;
  173.             Code = Msg->Code;
  174.             Object = Msg->IAddress;
  175.             Qualifier = Msg->Qualifier;
  176.             X = Msg->MouseX;
  177.             Y = Msg->MouseY;
  178.             ReplyMsg(Msg);
  179.             if (Class == MOUSEBUTTONS && Code == SELECTDOWN)    {
  180.                 chg = TRUE;
  181.                 if( Y > Window->Height / 2)    {
  182.                     ScrollUp();
  183.                 }
  184.                 else ScrollDown();
  185.             }
  186.             else if(Class == GADGETUP && Object == (APTR)&IconGadget) {
  187.                 NW.LeftEdge = Window->LeftEdge;
  188.                 NW.TopEdge = Window->TopEdge;
  189.                 NW.Width = Window->Width;
  190.                 NW.Height = Window->Height;
  191.                 CloseWindow(Window);
  192.                 Window = NULL;
  193.                 iconify (&iconx, &icony, 24, 10, NULL, &iconbord, 1);
  194.                 if( !(Window = OpenWindow(&NW))) {
  195.                     CleanUp();    
  196.                 }
  197.                 else {
  198.                     RastPort = Window->RPort;
  199.                     ReFill();
  200.                 }
  201.             }
  202.             else if(Class == RAWKEY) {
  203.                 chg = TRUE;
  204.                 if(Code == 0x45) 
  205.                     CleanUp();
  206.                 if(Code == 0x4D) { 
  207.                     if(Qualifier == 0x8001 || Qualifier == 0x8002) {
  208.                         if(CurrentTopLine+MaxLine-1 < Number - MaxLine) 
  209.                             CurrentTopLine += MaxLine -1;
  210.                         else 
  211.                             CurrentTopLine = Number - MaxLine;
  212.                         SetAPen (RastPort, 0);
  213.                         RectFill (RastPort,3,10,Window->Width-7,Window->Height-14);
  214.                         SetAPen (RastPort, 1);
  215.                         for ( j = 0; j < Number && j < MaxLine; j++)
  216.                             WriteLine(CurrentTopLine+j, (LONG) j*8 + 17);
  217.                     }
  218.                     else {
  219.                         ScrollUp();
  220.                         continue;
  221.                     }
  222.                 }
  223.                 if (Code == 0x4C) {
  224.                     if(Qualifier == 0x8001 || Qualifier == 0x8002) {
  225.                         if (CurrentTopLine-MaxLine-1 >0 )
  226.                             CurrentTopLine -= MaxLine -1;
  227.                         else
  228.                             CurrentTopLine = 0;
  229.                         SetAPen (RastPort, 0);
  230.                         RectFill (RastPort,3,10,Window->Width-7,Window->Height-14);
  231.                         SetAPen (RastPort, 1); 
  232.                         for ( j = 0; j < Number && j < MaxLine; j++)
  233.                             WriteLine(CurrentTopLine+j, (LONG) j*8 + 17);
  234.                     }
  235.                     else {
  236.                         ScrollDown();
  237.                         continue;
  238.                     }
  239.                 }
  240.             }
  241.             else if(Class == NEWSIZE)     {
  242.                 MaxColumn = (Window->Width - 6) / 8;
  243.                 MaxLine = (Window->Height - 22) / 8;
  244.                 ReFill();
  245.             }
  246.             else if(Class == CLOSEWINDOW)
  247.                 CleanUp();
  248.         }
  249.     }
  250. }
  251.  
  252. char **findtools(name,lock)                   /* Lecture du Tool Type */
  253. char *name;
  254. BPTR lock;
  255. {
  256.     BPTR olddir;
  257.     char **tools = NULL;
  258.  
  259.     if(lock == NULL) return (NULL);
  260.         olddir = (BPTR)CurrentDir(lock);
  261.     if((diskobj=GetDiskObject(name)) != NULL)
  262.         tools=diskobj->do_ToolTypes;
  263.     return(tools);
  264. }
  265.  
  266. ScrollUp()                            /*    Scroll BitMap upward. */
  267. {
  268.     StartTopLine=CurrentTopLine;
  269.     while (1)    {
  270.         if ((Msg = (struct IntuiMessage *) GetMsg(Window->UserPort)) != 0L) {
  271.             if (Msg->Code & 0x80)    {
  272.                 ReplyMsg (Msg);
  273.                 return;
  274.             }
  275.             else {
  276.                 if(Msg->Class == NEWSIZE)                {
  277.                     MaxColumn = (Window->Width - 6) / 8;
  278.                     MaxLine = (Window->Height - 22) / 8;
  279.                     ReFill();
  280.                 }
  281.                 else            {
  282.                     if(Msg->Class == CLOSEWINDOW)    {
  283.                         ReplyMsg (Msg);
  284.                         CleanUp();
  285.                     }
  286.                 }
  287.             }
  288.             ReplyMsg (Msg);
  289.         }
  290.         if (CurrentTopLine < Number - MaxLine)    {
  291.             CurrentTopLine += 1L;
  292.         }
  293.         else return;
  294.  
  295.         ClipBlit(RastPort,3L,19L,RastPort,3L,11L,(LONG)(Window->Width-6),(LONG)(((Window->Height-22)&0xfff8)-8),0xC0L);
  296.         WriteLine(CurrentTopLine + MaxLine - 1L, (LONG)(MaxLine * 8 + 9));
  297.     }
  298. }
  299.  
  300. ScrollDown()                            /*    Scroll BitMap downward.*/
  301. {
  302.     StartTopLine=CurrentTopLine;
  303.     while (1)    {
  304.         if ((Msg = (struct IntuiMessage *)GetMsg(Window->UserPort)) != 0L) {
  305.             if (Msg->Code & 0x80) {
  306.                 ReplyMsg (Msg);
  307.                 return;
  308.             }
  309.             else {
  310.                 if(Msg->Class == NEWSIZE)    {
  311.                     MaxColumn = (Window->Width - 6) / 8;
  312.                     MaxLine = (Window->Height - 22) / 8;
  313.                     ReFill();
  314.                 }
  315.                 else    {
  316.                     if(Msg->Class == CLOSEWINDOW) {
  317.                         ReplyMsg (Msg);
  318.                         CleanUp();
  319.                     }
  320.                 }
  321.             }
  322.             ReplyMsg (Msg);
  323.         }
  324.         if (CurrentTopLine != 0)    {
  325.             CurrentTopLine--;
  326.         }
  327.         else return;
  328.  
  329.         ClipBlit(RastPort,3L,11L,RastPort,3L,19L,(LONG)(Window->Width-6),(LONG)(((Window->Height-22)&0xfff8)-8),0xC0L);
  330.         WriteLine (CurrentTopLine, 17L);
  331.     }
  332. }
  333.  
  334. WriteLine(Line, Y)    /*This is the routine that draw the text correctly.*/
  335. LONG Line, Y;
  336. {
  337.     WORD i, j, k;
  338.     BYTE LineBuffer[127];
  339.  
  340.     Move(RastPort, 3L, Y);
  341.     SetAPen(RastPort, 1);
  342.     SetBPen(RastPort, 0);
  343.  
  344.     for ( i = 0, j = 0, k = 0; k < MaxColumn; i++)    {
  345.         if ( Memory[Line][i] == 10)    {    /*** Check for LF ***/
  346.             Text (RastPort, LineBuffer, (LONG)j);
  347.             SetAPen (RastPort, 0);
  348.             RectFill (RastPort, (LONG)(k * 8 + 3), Y - 6L, (LONG)(Window->Width - 3), Y + 1L);
  349.             SetAPen (RastPort, 1);
  350.             return;
  351.         }
  352.         else {
  353.             if (Memory[Line][i] == 9) {    /*** Check for Tab ***/
  354.                 do    {
  355.                     if (k < MaxColumn)        {
  356.                         LineBuffer[j++] = 32;
  357.                         k++;
  358.                     }
  359.                     else {
  360.                         Text(RastPort, LineBuffer, (LONG)j);
  361.                         return;
  362.                     }
  363.                 }
  364.                 while (k % 8 != 0);
  365.             }
  366.             else {
  367.                 LineBuffer[j++] = Memory[Line][i];
  368.                 k++;
  369.             }
  370.         }    /*** Close else of if LF ***/
  371.     }    /*** Close line writing loop ***/
  372.     Text (RastPort, LineBuffer, (LONG)j);
  373. }
  374.  
  375. ReFill()            /*    Refill window with text when resized.*/
  376. {
  377.     register LONG j, i;
  378.     for ( j= CurrentTopLine, i=0 ; j < Number && j < CurrentTopLine+MaxLine; j++,i++) {
  379.         WriteLine(j, (LONG)(i * 8 + 17));
  380.     }
  381.     PrintIText(RastPort, &Mess, 10, Window->Height-10);    
  382.     SetAPen (RastPort, 0);
  383.     RectFill (RastPort,2,Window->Height-10,28,Window->Height-2);
  384.     SetAPen (RastPort, 1);
  385.     IconGadget.TopEdge = Window->Height-10;
  386.     RefreshGList(&IconGadget,Window,NULL,1);
  387. }
  388.  
  389. CleanUp()            /*    Release everything. */
  390. {
  391.     if (diskobj)        FreeDiskObject(diskobj);
  392.     if (Memory)         FreeMem (Memory,Number*4L);
  393.     if (Buffer)            FreeMem (Buffer,Len+1);
  394.     if (Window)            CloseWindow (Window);
  395.     if (IconBase)         CloseLibrary(IconBase);
  396.     if (GfxBase )         CloseLibrary(GfxBase);
  397.     if (IntuitionBase)    CloseLibrary(IntuitionBase);
  398.     exit (0);
  399. }
  400.